<?php

	class RequestDataAccessMySQL implements iRequestDataAccess {
		
		public function buildObject($result) {

			$serviceRequest = new Request();
			
			$serviceRequest->setRequestId($result->requestId); 
			$serviceRequest->setRoomGuestId($result->roomGuestId);
			$serviceRequest->setRequestDate(formatDate($result->dateTime));
			$serviceRequest->setResponseDate(formatDate($result->responseDateTime)); 
					
			$service = new Service(); 	
			$service->setServiceId($result->serviceId); 
			$service->setName($result->description); 
			$serviceRequest->setService($service);
			
			$employee = new Employee();
			$employee->setEmployeeId($result->employeeId);
			if (property_exists($result, 'name')) {
				$employee->setName($result->name);
			}
			$serviceRequest->setEmployee($employee);
			
			if (property_exists($result, 'userName')) {
				$userRequest = new User();
				$userRequest->setUserId($result->userId);
				$userRequest->setName($result->userName);
				$serviceRequest->setUserRequest($userRequest);
			}

			if (property_exists($result, 'regionName')) {
				$serviceRequest->setRegionName($result->regionName);
			}
			//TODO setRequestItemList
			
			return $serviceRequest;
		}
		
		public function save($serviceRequest, $dataBaseConnection) {
			
			$parameters["SERVICEID"] = $serviceRequest->getService()->getServiceId();
			$parameters["ROOMGUESTID"] = $serviceRequest->getRoomGuestId();
			
			$query = buildQuery("queryInsertRequest", $parameters);
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
						
					return $dataBaseConnection->getLastInsertId();
				}
				else {
					return -1;
				}
			} else {
				return -1;
			}
			
		}
		
		public function updateRequest($serviceRequest, $dataBaseConnection) {
			
			$parameters["REQUESTID"]    = $serviceRequest->getRequestId();
			$parameters["EMPLOYEEID"]    = $serviceRequest->getEmployee()->getEmployeeId();
						
			
			$query = buildQuery("queryUpdateRequest", $parameters);
			
			
			if (!is_bool($query)) {
				$result = $dataBaseConnection->query($query);
				
				return $result;
			} else {
				return false;
			}
			
		}
		
		public function deleteRequest($serviceRequest, $dataBaseConnection) {
						
			
			$parameters["REQUESTID"]    = $serviceRequest->getRequestId();
					
			$query = buildQuery("queryDeleteRequest", $parameters);
			if (!is_bool($query)) {
				$result = $dataBaseConnection->query($query);
				
				return $result;
			} else {
				return false;
			}
		}
		
		public function selectRequestsByUserId($userId, $dataBaseConnection) {
			$resultArray = array();
			
			$parameters["USERID"] = $userId;
			
			$query = buildQuery("querySelectRequestsByUserId", $parameters);
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
					while($row = mysql_fetch_object($result)) {
						array_push($resultArray, $this->buildObject($row));	
					}
					return $resultArray;	
				}
				else {
					return NULL;
				}
			} else {
				return NULL;
			}
		}
		
		public function selectRequestsByEmployeeId($employeeId, $dataBaseConnection) {
			
			$resultArray = array();
			
			$parameters["EMPLOYEEID"] = $employeeId;
			
			$query = buildQuery("querySelectRequestsByEmployeeId", $parameters);
			
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
					while($row = mysql_fetch_object($result)) {
						array_push($resultArray, $this->buildObject($row));	
					}
					return $resultArray;	
				}
				else {
					return NULL;
				}
			} else {
				return NULL;
			}
		}
		
	}

?>